#coding=utf-8
'''
数据处理静态方法
'''
import re
import datetime
from qturboframe.lib.report.log import logger

def deal_inputdata(inputdata_key, inputdata_value):
    '''
    对表单的输入数据根据#字段进行处理

    :Args:
     - inputdata_key: 输入数据的key值
     - inputdata_value: 输入数据的value值

    :Returns:
     - key: 去掉key值中的#后部分
     - value: 去掉value值中的#后部分
     - flag: 根据#后部分返回的操作标签，默认返回assign填充
     - label_seq: 根据#后部分返回的元素顺序下标，默认返回1
    '''
    flag = ''
    label_seq = 1
    key_suffix = ''
    value_suffix = ''
    keylist = inputdata_key.split('#')
    valuelist = inputdata_value.split('#')
    key = keylist[0]
    value = valuelist[0].split(',')
    try:
        if len(keylist) == 2:
            key_suffix = keylist[1]
            # 获取元素序号
            if re.search('\d+', key_suffix):
                label_seq = int(re.search('\d+', key_suffix).group())
        if len(valuelist) == 2:
            value_suffix = valuelist[1]
        # 判断检查标记
        if value_suffix != '' or key_suffix != '':
            if 'R' in key_suffix or 'R' in value_suffix:
                # logger.getlogger().debug(key + '做只读标记。')
                flag = 'readonly'
            if 'D' in key_suffix or 'D' in value_suffix:
                # logger.getlogger().debug(key + '做默认值标记。')
                flag = flag + 'default'
            elif ('0' in value_suffix or '全不选' in value) and flag == '':
                # logger.getlogger().debug(key + '做清空标记。')
                flag = 'clear'
            elif '全选' in value:
                # logger.getlogger().debug(key + '做全选标记。')
                flag = 'all'
            elif 'M' in key_suffix or 'M' in value_suffix:
                # logger.getlogger().debug(key + '做必填项标记。')
                flag = 'must'
            else:
                # logger.getlogger().debug(key + '未识别检查标识或者标识排列出错，默认做填充标记。')
                flag = 'assign'
        else:
            # logger.debug(key + '做填充标记。')
            flag = 'assign'
    except Exception as msg:
        logger.warn('输入数据格式不正确：' + str(msg))
    return key,value,flag,label_seq

def deal_datetime(date=str):
    '''
    处理日期格式

    :Args:
     - date: 输入数据中的原始日期格式

    :Returns:
        返回统一的日期格式：year/month/day
    '''
    try:
        date = datetime.datetime.strptime(date,"%Y%m%d")
    except ValueError:
        try:
            date = datetime.datetime.strptime(date, "%Y-%m-%d")
        except ValueError:
            date = datetime.datetime.strptime(date, "%Y/%m/%d")
    return date.strftime("%Y/%m/%d")

def deal_time(time=str):
    '''
    处理时间格式

    :Args:
     - time: 输入数据中的原始时间格式

    :Returns:
        返回统一的日期格式：hour:minute:second
    '''
    try:
        time = datetime.datetime.strptime(time, "%H:%M:%S")
    except ValueError:
        time = datetime.datetime.strptime(time, "%H:%M")
    return time.strftime("%H:%M:%S")

def get_same_value_in_lists(list1, list2):
    '''
    获取列表中相同值

    :Args:
     - list1:待比较的list1
     - list2:待比较的list2

    :Returns:
        返回相同值列表

    :Usage:
        get_same_value_in_lists([1,2,3],[1,3,5]) ==> [1,3]
    '''
    if len(list1) == 0 or len(list2) == 0:
        return []
    temp_list = list()
    for i in list1:
        for j in list2:
            if i == j:
                temp_list.append(i)
    return temp_list

if __name__ == '__main__':
    key, value, flag, label_seq = deal_inputdata('abc#02', '111222')
    print(key, value, flag, label_seq)
    # time = r'4:5'
    # aa = dealTime(time)
    # print(aa)